<?php
namespace app\merchant\controller;

use app\merchant\model\MerchantModel;
use tp5_lib\account\logic\AccountLogic;
use tp5_lib\user_api\controller\LoginController;
use tp5_lib\user\model\UserModel;
use app\payment\model\PaymentModel;

class ApiController extends LoginController
{
    /**
     * 检查是否有商家权限
     * 必须已提交资料并且通过审核
     */
    protected function checkMerchant()
    {
        return true;
    }
    public function login()
    {
        $data=input();
        if(!$this->validate->scene("login")->check($data)){
            $this->error($this->validate->getError());
        }
        $user=$this->logic->login($data["username"],$data["password"]);
        if(!$user){
            $this->error($this->logic->getError());
        }

        $model=new MerchantModel();
        if(!$record=$model->db()->where(["user_id"=>$user["id"]])->order("id","desc")->find()){
            $user["merchant"]["status"] = "-1";
        }
        else{
            session("merchant_info",$record);
            $user["merchant"] = $record;
        }
        if($user["merchant"]["status"] != "1"){
            $this->result($user,-1,"请先提交资料或等待审核");
        }

        $account=new AccountLogic();
        if(!$account->hasWithdrawWay($user)){
            $this->result($user,-2,"请先绑定提现方式");
        }

        $this->result($user,1);
    }
    /**
     * 提交资料
     */
    public function submit()
    {
        $user=$this->checkLogin();
        $data=input();
        $data["user_id"]=$user["id"];
        $data["time"]=time();
        unset($data["token"]);

        $upload_dir=env("root_path").'/public/uploads';
        if(!file_exists($upload_dir)){
            mkdir($upload_dir);
        }
        $upload_dir.="/merchant";
        if(!file_exists($upload_dir)){
            mkdir($upload_dir);
        }
        $path="uploads/merchant/";
        $save_name=$user["id"]."-".time()."-";

        /** @var File[] $files */
        $files = $this->request->file();
        if(isset($files["license"])){
            if($info=$files["license"]->move($upload_dir, $save_name."license")){
                $data["license"]=$path . $info->getSaveName();
            }
            else{
                $this->error($files["license"]->getError());
            }
        }
        if(isset($files["idcard"])){
            if($info=$files["idcard"]->move($upload_dir, $save_name."idcard")){
                $data["idcard"]=$path . $info->getSaveName();
            }
            else{
                $this->error($files["idcard"]->getError());
            }
        }

        $model=new MerchantModel();
        if(!$model->db()->insert($data)){
            $this->error("保存失败");
        }
        $this->success("提交成功");
    }
    /**
     * 获取资料
     */
    public function info()
    {
        $user=$this->checkLogin();
        $model=new MerchantModel();
        $record=$model->db()->where(["user_id"=>$user["id"]])->order("id","desc")->find();
        $this->result($record,1);
    }
    /** 销售笔数，总销售额，可提现 */
    public function statistics()
    {
        $user=$this->checkLogin();
        // $user=UserModel::db()->where("id",$user["id"])->find();
        // if(!$user){
        //     $this->error("用户不存在");
        // }
        $count=PaymentModel::where(["payee"=>$user["id"],"status"=>"1"])->count();//销售笔数
        $total=PaymentModel::where(["payee"=>$user["id"],"status"=>"1"])->sum("amount");//总收入
        $money=UserModel::where(["id"=>$user["id"]])->field("money")->find();
        $this->result([
            "sale_count"=>$count,
            "sale_total"=>$total,
            "money"=>$money["money"],
        ],1);
    }
}
